package com.toman.templateActivity.controler;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DensityUtil;
import com.toman.Util.DialogUtils;
import com.toman.Util.TimeUtil;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.bean.TemplateInfBean;
import com.toman.bean.TemplateInfBeanPreHandler;
import com.toman.widget.ClearEditText;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import lombok.Data;

/**
 * Created by 邹苏隆 on 2018/11/5.
 * description：
 */
@Data
public class TempInfAdapter implements TempInterface {
    private List<TemplateInfBean> list = new ArrayList<>();
    //    子布局
    private List<TempInterface> tempInterfaces = new ArrayList<>();
    private Context context;
    LinearLayout ll;
    boolean edit = false;

    LinkedList<Object> linkedList = new LinkedList<>();

    public List<TemplateInfBean> getList() {
        return list;
    }

    public void setList(List<TemplateInfBean> list) {
        this.list = list;
    }

    public LinearLayout getLl() {
        return ll;
    }

    public void setLl(LinearLayout ll) {
        this.ll = ll;
    }

    public TempInfAdapter(List<TemplateInfBean> list, Context context, LinearLayout linearLayout) {
        this.list.addAll(list);
        this.context = context;
        ll = linearLayout;
        for (TemplateInfBean templateInfBean : list) {
            linkedList.add(templateInfBean);
        }        setContent();
    }
    public TempInfAdapter( Context context, LinearLayout linearLayout) {
        this.context = context;
        ll = linearLayout;

    }
    public TempInfAdapter(List<TemplateInfBean> list, Context context, LinearLayout linearLayout, boolean edit) {
        this.list.addAll(list);
        this.context = context;
        ll = linearLayout;
        this.edit = edit;
        for (TemplateInfBean templateInfBean : list) {
            linkedList.add(templateInfBean);
        }
        setContent();
    }

    public TempInfAdapter(Context context, LinearLayout linearLayout, List<TempInterface> tempInterfaces) {
        this.context = context;
        this.tempInterfaces.addAll(tempInterfaces);
        ll = linearLayout;
        for (TempInterface tempInterface : tempInterfaces) {
            ll.addView(tempInterface.createView(context));
        }
        linkedList.add(tempInterfaces);

    }

    public void addAdapters(List<TempInterface> tempInterfaces) {
        this.tempInterfaces.addAll(tempInterfaces);
        for (TempInterface tempInterface : tempInterfaces) {
            ll.addView(tempInterface.createView(context));
        }
        linkedList.add(tempInterfaces);
    }

    public void addList(List<TemplateInfBean> list) {
        this.list.addAll(list);
        setContent();
        for (TemplateInfBean templateInfBean : list) {
            linkedList.add(templateInfBean);
        }    }


    private void setContent() {
        TempView tempView=new TempView();
        for (TemplateInfBean bean : list) {
tempView.createView(bean,edit,list,ll,context);
        }
    }
    private void setContent(TemplateInfBean bean) {
        TempView tempView=new TempView();
        tempView.createView(bean,edit,list,ll,context);

    }


    public void edit(boolean b) {
        ll.removeAllViews();
        edit = b;
        setContent();
    }

    public void refresh(List<TemplateInfBean> list) {
        ll.removeAllViews();
        this.list = list;
        setContent();
    }
    public void refresh() {
        ll.removeAllViews();
        for (Object o : linkedList) {
            if (o instanceof List){
                List<TempInterface> tempInterfaces = (List<TempInterface>) o;
                for (TempInterface tempInterface : tempInterfaces) {
                    tempInterface.setEdit(edit);
                    ll.addView(tempInterface.createView(context));
                }
            }
            else if (o instanceof TemplateInfBean){
                TemplateInfBean TemplateInfBean = (TemplateInfBean) o;
                setContent( TemplateInfBean);
            }
        }
    }

    public boolean isNull() {
        List<TemplateInfBean> list = new ArrayList<>();
        list.addAll(this.list);
        for (TempInterface tempInterface : tempInterfaces) {
            list.addAll(tempInterface.getTempBeanList());
        }
        for (int i = 0; i < list.size(); i++) {
            TemplateInfBean bean = list.get(i);

            if (bean.getName() != null && bean.isNecessary() && bean.getResult() == null) {
                ToastUtil.makeText(context, bean.getTitle() + "不得为空");
                return true;
            }

        }
        return false;

    }

    public   List<TemplateInfBean> getAllList() {
        List<TemplateInfBean> list = new ArrayList<>();
        list.addAll(this.list);
        for (TempInterface tempInterface : tempInterfaces) {
            list.addAll(tempInterface.getTempBeanList());
        }
 return list;

    }
    public void refresh(boolean edit) {
        this.edit = edit;
        ll.removeAllViews();
        for (Object o : linkedList) {
            if (o instanceof List){
                List<TempInterface> tempInterfaces = (List<TempInterface>) o;
                for (TempInterface tempInterface : tempInterfaces) {
                    tempInterface.setEdit(edit);
            ll.addView(tempInterface.createView(context));
        }
            }
            else if (o instanceof TemplateInfBean){
                TemplateInfBean TemplateInfBean = (TemplateInfBean) o;
                setContent( TemplateInfBean);
            }
        }
//        setContent();

    }

    @Override
    public View createView(Context context) {
        return null;
    }

    @Override
    public List<TemplateInfBean> getTempBeanList() {
        return list;
    }
}
